package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/9 14:12
 */
public class M0402 {

    // https://leetcode-cn.com/problems/minimum-height-tree-lcci/
    // 有序数组生成一颗搜索树, 高度尽量小.  无疑保证左右尽量平衡
    public static TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums, 0, nums.length-1);
    }

    private static TreeNode dfs(int[] nums, int l, int r) {
        if(l <= r) {
            int m = l + ((r-l) >> 1);
            TreeNode head = new TreeNode(nums[m]);
            head.left = dfs(nums, l, m-1);
            head.right = dfs(nums, m+1, r);
            return head;
        }
        return null;
    }


    public static void main(String[] args) {

    }

}
